<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%--测试接口--%>
<%
    JSONObject jsonData = new JSONObject();
    String code = Util.null2String(request.getParameter("code"));
    RecordSet rs = new RecordSet();
    rs.executeQuery("select id,requestid,invoicenumber,invoicetype,billingdate,seller,taxincludedprice," +
            "pricewithouttax,userid_new,billingdate from fnainvoiceledger where invoiceCode='"+code+"'");
    if(rs.next()){
        JSONObject paramJson = new JSONObject();
        JSONArray detailList = new JSONArray();
        JSONObject detailJson = new JSONObject();
        String id = Util.null2String(rs.getString("invoicenumber"));
        String seller = Util.null2String(rs.getString("seller"));
        detailJson.put("wbid",Util.null2String(rs.getString("requestid")));
        detailJson.put("operate","1");
        detailJson.put("fphm",Util.null2String(rs.getString("invoicenumber")));
        detailJson.put("fplx",Util.null2String(rs.getString("invoicetype")));
        detailJson.put("kprq",Util.null2String(rs.getString("billingdate")));
        detailJson.put("csbh",getGysbm(seller));
        detailJson.put("fpsl",getFpsl(id));
        detailJson.put("fpje",Util.null2String(rs.getString("taxincludedprice")));
        detailJson.put("csje",Util.null2String(rs.getString("pricewithouttax")));
        detailJson.put("kprm",Util.null2String(rs.getString("userid_new")));
        detailJson.put("kpsj",Util.null2String(rs.getString("billingdate")));
        detailList.add(detailJson);
        JSONObject dataJson = new JSONObject();
        dataJson.put("Details",detailList);
        JSONObject DataJson = new JSONObject();
        DataJson.put("Data",dataJson);
        paramJson.put("data",DataJson);
        String returnStr = sendOKHttp3Request(paramJson.toJSONString());
        jsonData.put( "result", returnStr );
    }

    out.print( jsonData.toJSONString() );
%>
<%!
    public static String sendOKHttp3Request(String paramJson){
        String result = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = RequestBody.create(mediaType, "");
            String url = "http://58.241.13.10:11895/Api/PutData/ExecProcedureWithReturnJsonError";
            String dataflow = "dataflow=泛微oa→RZERPv4_铁建工科";
            String actionCode = "actionCode=put_csxxb";

            String data = "&data="+paramJson;
            Request request = new Request.Builder()
                    .url(url+"?"+dataflow+"&"+actionCode+"&"+data)
                    .method("POST", body)
                    .addHeader("Accept", "*/*")
                    .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    public static String getGysbm(String gysmc){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select gysbm from uf_gystz where gysmc='"+gysmc+"'");
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public static String getFpsl(String id){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select sum(unitnumber) from fnainvoiceledgerdetail where mainid='"+id+"'");
        rs.next();
        return Util.null2String(rs.getString(1));
    }
%>





